//清空bug详情表单
function testProjectBugCleanForm() {
    testProjectBugDetailForm.reset();
    testProjectBugDetailForm.down('#toTrq').toTrq = null;
    testProjectBugAttachedGridStore.removeAll();
}

//获取bug详细信息
function getTestProjectBugData(id) {
    var result;
    Ext.Ajax.request({
        url: zuulUrl + '/' + testServiceName + '/testProject/bug/detail/' + id,
        method: 'get',
        async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
        success: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            result = obj;
        },
        failure: function (response, opts) {
            var obj = Ext.decode(response.responseText);
            if (obj.code == 401 || obj.code == 402 || obj.code == 403 || obj.code == 406 || obj.code == 407)
                Ext.Msg.alert("错误", obj.message);
            else
                Ext.Msg.alert("错误", "该功能暂时不可用！");
        }
    });
    return result;
}

//将表单数据填充到表单
function testProjectBugPutDataInto(data) {
    //如果是作者的话就可以修改
    testProjectBugBanEditor(data.testBug.author != currentUserId);
    testProjectBugDetailForm.down('#title').setValue(data.testBug.title);
    testProjectBugDetailForm.down('#author').setValue(data.testBug.authorName);
    testProjectBugDetailForm.down('#author').author = data.testBug.author;
    testProjectBugDetailForm.down('#toTrq').setValue(data.testBug.trqName);
    testProjectBugDetailForm.down('#toTrq').toTrq = data.testBug.toTrq;
    testProjectBugDetailForm.down('#createDate').setValue(data.testBug.createDate);
    testProjectBugDetailForm.down('#updateDate').setValue(data.testBug.updateDate);
    testProjectBugDetailForm.down('#describe').setValue(data.testBug.describe);
    testProjectBugTabPanel.down('#grade').setValue(data.testBug.grade);//填充该bug的分数
    testProjectBugAttachedGridStore.setData(data.attacheds);
}

//将表单设置为不可编辑
function testProjectBugBanEditor(isBan) {
    testProjectBugDetailForm.down('#title').setReadOnly(isBan);
    testProjectBugDetailForm.down('#trqButton').setHidden(isBan);
    testProjectBugDetailForm.down('#describe').setReadOnly(isBan);
    testProjectBugTabPanel.down('#saveButton').setHidden(isBan);
}

//点击查看详情之后要做的事
function testProjectBugSeeDetailClick(record) {
    Ext.getCmp('centerRegion').removeAll(false);
    Ext.getCmp('centerRegion').add(testProjectBugTabPanel);
    testProjectBugTabPanel.setActiveTab(0);
    testProjectBugCleanForm();
    var data = getTestProjectBugData(record.get('id'));
    testProjectBugPutDataInto(data);
    testProjectBugIsCreate = 0;
    testProjectBugTabPanel.bugId = record.get('id');
}

//点击添加bug之后要做的事情
function testProjectBugAddClick() {
    Ext.getCmp('centerRegion').removeAll(false);
    Ext.getCmp('centerRegion').add(testProjectBugTabPanel);
    testProjectBugTabPanel.setActiveTab(0);
    testProjectBugCleanForm();
    testProjectBugBanEditor(false);
    testProjectBugDetailForm.down('#author').setValue(currentUser);
    testProjectBugDetailForm.down('#author').author = currentUserId;
    testProjectBugIsCreate = 1;
}

//点击删除bug之后要做的事情
function testProjectBugDeleteClick() {
    var item = testProjectBugGrid.getSelectionModel().getSelected();// 得到被选择的行的数组.
    if (item.length == 0)
        Ext.Msg.alert('错误', '请选择要删除的bug！');
    else {
        Ext.MessageBox.confirm('删除', '你确定要将该bug删除吗', function (btn) {
            if (btn === 'yes') {
                var ids = new Array();
                for (var i = 0, length = item.items.length; i < length; i++) {
                    if (item.items[i].data.author != currentUserId) {
                        Ext.Msg.alert("错误", "只能删除你自己提交的bug!");
                        return;
                    }
                    ids.push(item.items[i].data.id);
                }
                Ext.Ajax.request({
                    url: zuulUrl + '/' + testServiceName + '/testProject/bug/del/' + currentUserId,
                    method: 'put',
                    async: true, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
                    defaultXdrContentType: 'application/json',
                    jsonData: JSON.stringify(ids),
                    success: function (response, opts) {
                        Ext.Msg.alert("成功", "删除成功");
                        testProjectBugGridStore.remove(item.items);
                    },
                    failure: function (response, opts) {
                        var obj = Ext.decode(response.responseText);
                        if (obj.code == 401 || obj.code == 402 || obj.code == 403 || obj.code == 406 || obj.code == 407)
                            Ext.Msg.alert("错误", obj.message);
                        else
                            Ext.Msg.alert("错误", "该功能暂时不可用！");
                    }
                });
            }
        });
    }
}

//当文件上传框变化之后触发的函数
function bugGetFilePath() {
    var fileEl = document.getElementById('bugAttaInput');
    testProjectBugFileArray.push(fileEl.files[0]);
    var filePath = fileEl.value;    //js中无法获取文件的真是路径
    var fileName = filePath.substring(filePath.lastIndexOf('\\') + 1); //文件名
    var newRecord = {
        id: Ext.create("MyApp.data.MyModelX").data.id.replace(/-/g, ""),
        name: fileName,
        isCreate: true
    };
    testProjectBugAttachedGridStore.add(newRecord);
}

//保存bug
function testProjectBugSave() {
    if (testProjectBugDetailForm.isValid() && testProjectBugDetailForm.down('#toTrq').toTrq != null) {
        testProjectBugTabPanel.down('#saveButton').setDisabled(true);
        var bugId;
        var fd = new FormData();
        if (testProjectBugIsCreate == 1)//如果现在是新建操作
            bugId = getUuid();//自己生成Id
        else {
            bugId = testProjectBugTabPanel.bugId;
            fd.append("currentUserId", currentUserId);
            var attaList = testProjectBugAttachedGridStore.getData();
            Ext.each(attaList.items, function (record) {//向formData添加附件Id列表
                fd.append('newAttachedIds', record.data.id);
            });
        }
        var title = testProjectBugDetailForm.down('#title').getValue();
        var toTrq = testProjectBugDetailForm.down('#toTrq').toTrq;
        var describe = testProjectBugDetailForm.down('#describe').getValue();
        fd.append("id", bugId);
        fd.append("title", title);
        fd.append("toTrq", toTrq);
        fd.append("describe", describe);
        fd.append("projectId", currentProjectId);
        fd.append("author", currentUserId);
        for (var i = 0, length = testProjectBugFileArray.length; i < length; i++) {//向formData添加附件文件
            fd.append("attachedFiles", testProjectBugFileArray[i]);
        }
        Ext.Ajax.request({
            url: zuulUrl + '/' + testServiceName + '/testProject/bug',
            async: false,   //ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            method: testProjectBugIsCreate == 1 ? 'post' : 'put',
            rawData: fd,
            headers: {
                "Content-Type": null   /* 最重要的部分，将Content-Type设置成null，ExtJs则会由内部的XMLHTTPRequest自动生成包含boundary的Content-Type，否则在使用rawData的情况下，ExtJs会将Content-Type设置成text/plain */
            },
            success: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                Ext.Msg.alert('成功', '保存成功');
                testProjectBugTabPanel.bugId = bugId;
                testProjectBugIsCreate = 0;//标注现在是编辑操作
                Ext.each(testProjectBugAttachedGridStore.getData().items, function (record) {
                    record.data.isCreate = false;//将新上传的附件标为不是新添加的
                });
                testProjectBugFileArray = [];//清空formData储存内容
                testProjectBugDetailForm.down('#createDate').setValue(obj.createDate);
                testProjectBugDetailForm.down('#updateDate').setValue(obj.updateDate);
            },
            failure: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                if (obj.code == 407)
                    Ext.Msg.alert("错误", obj.message);
                else
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
        testProjectBugTabPanel.down('#saveButton').setDisabled(false);
    } else
        Ext.Msg.alert("错误", "bug信息未填完全！");
}

//panel点击返回之后要做的事情
function testProjectBugBackClick() {
    Ext.getCmp('centerRegion').removeAll(false);
    testProjectBugGridStore.reload();
    Ext.getCmp('centerRegion').add(testProjectBugGrid);
}

//获取孩子节点
function bugGetTrqChildren(record) {
    if (record.get('leaf') == false && record.get('isHas') == 0) {
        Ext.Ajax.request({
            url: zuulUrl + '/' + testServiceName + '/common/testRequirement/children/' + record.get('id'),
            method: 'get',
            async: false, // ASYNC 是否异步( TRUE 异步 , FALSE 同步)
            success: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                Ext.each(obj, function (node) {// 获得数据之后先将title的值赋值给text
                    node.text = node.title;
                });
                record.appendChild(obj);
                if (record.data.leaf == false && record.data.expanded == false)
                    record.set('expanded', true);
                record.set("isHas", 1);
            },
            failure: function (response, opts) {
                var obj = Ext.decode(response.responseText);
                if (obj.code == 401 || obj.code == 402 || obj.code == 403)
                    Ext.Msg.alert("错误", obj.message);
                else
                    Ext.Msg.alert("错误", "该功能暂时不可用！");
            }
        });
    }
}